/*
 * Escalera.cpp
 *
 *  Created on: 23/04/2013
 *      Author: juan
 */

#include "Escalera.h"
#include "../exceptions/ArgumentInvalidError.h"
#include <cmath>
#define CANT_ESCALONES 20
#define ANCHO_ESCALON (1.5)
#define ALTO_ESCALON (0.2)

using namespace std;


Escalera::Escalera(GLuint  program):Figure(program) {
	create_components(program);
}

void Escalera::create_components(GLuint  program) {
	this->escalones = new std::list<Cube*>();
	this->primerEscalon = new Cube(program);
	this->ultimoEscalon = new Cube(program);
	
	for (int i = 0; i <= CANT_ESCALONES; i++) {
		escalones -> push_front(new Cube(program));
	}
}

Escalera::~Escalera() {
	for ( std::list<Cube*>::iterator it = escalones->begin(); it != escalones->end(); it++) {
		delete (*it);
	}
}

void Escalera::draw(GLuint program, glm::mat4 & world_matrix) {
	glm::mat4 matriz_escalones = glm::scale( world_matrix, glm::vec3(ALTO_ESCALON,ANCHO_ESCALON,ALTO_ESCALON));
	glm::mat4 m = glm::mat4(1.0f);
	glm::mat4 matriz_descanso = glm::scale( world_matrix, glm::vec3(1.0f,ANCHO_ESCALON,ALTO_ESCALON));
	matriz_descanso = glm::translate( matriz_descanso, glm::vec3(0.0f,0.0f,1.0f));
	primerEscalon -> draw(program,matriz_descanso);
	m = glm::translate(matriz_escalones, glm::vec3(0.0f,0.0f,1.0f));
	for ( std::list<Cube*>::iterator it = escalones->begin(); it != escalones->end(); it++) {
		m = glm::translate(m, glm::vec3(1.0f,0.0f,1.0f));
		(*it) -> draw(program,m);
	}
	matriz_descanso = glm::translate(matriz_descanso, glm::vec3((CANT_ESCALONES+3)*ALTO_ESCALON,0.0f,CANT_ESCALONES + 2));//Dibujo el último escalón, es mas ancho que el resto
	//El +2 es por el corrimiento inicial en el primer escalón. El +3 todavia no descubro porque.
	this->ultimoEscalon->draw(program,matriz_descanso);
}

void Escalera::set_texture_id(GLuint id) {
	for (std::list<Cube*>::iterator it = escalones->begin(); it != escalones->end(); it++) {
		Cube* aux = (*it);
		aux->set_texture_id(id);
	}

	primerEscalon->set_texture_id(id);
	ultimoEscalon->set_texture_id(id);
}
